CategoryUtil = {
    data: ${categoriesJson},
    exists: function(id){
        if(this.data[id]==undefined){
            return false;
        }
        return true
    },
    getItem: function(id){
        if(this.exists(id)){
            return this.data[id];
        }
    },
    getSub: function(id){
        if(!this.exists(id)){
                alert('can not find item with id ' + id);
                return null;
        }
        var obj = {};
        var exp = new RegExp('^' + id + '\\d{3}$');
        for(var i in this.data){
            if(exp.test(i)){
                obj[i] = this.data[i];
            }
        }
        for(var i in obj){
            return obj;
        }
        return null;
    },
    getParent: function(id){
        if(!this.exists(id)){
            alert('error in getParent, can not find item with id: ' + id);
            return null;
        }
        if(id.length<=3){
            //alert('No parent for the top level region.');    
            return null;
        }
        var pid = id.substring(0, id.length-3);
        
        if(!this.exists(pid)){
                alert('error in getParent, can not find item with id: ' + pid);
                return null;
        }
        return this.data[pid];
    },
    createCategorySelHtml: function(categories, selValue){
        if(categories==null){
            return '';
        }
        var html = '<select onchange="doCategorySelChange(this);"><option value="">请选择...</option>';
        for(var c in categories){
            var sel = "";
            if(selValue==c){
                sel = "selected";
            }
            html += '<option ' + sel + ' value="' + c + '">' + categories[c].name + '</option>';
        }
        html += '</select>';
        return html;
    },
    doCategorySelChange: function (e){
        var v = $(e).val();
        if(v!=''){
            $('#categoryId').val(v);
            while($(e).next().length>0){
                $(e).next().remove();
            }
            $(CategoryUtil.createCategorySelHtml(CategoryUtil.getSub(v))).appendTo($('#categorySelContainer'));
        }
        else{
            $('#categoryId').val('');
            while($(e).next().length>0){
                $(e).next().remove();
            }
        }
    },
    restoreSel: function(){
        var selectedCategoryId = $('#categoryId').val();
        if(selectedCategoryId==''){
            $(CategoryUtil.createCategorySelHtml(CategoryUtil.getSub('100'))).appendTo($('#categorySelContainer'));
        }
        else{
            var totalLength = 3;
            var pid;
            var selVal;
            while(totalLength<=selectedCategoryId.length){
                pid = selectedCategoryId.substring(0, totalLength);
                selVal = selectedCategoryId.substring(0, totalLength + 3);
                $(CategoryUtil.createCategorySelHtml(CategoryUtil.getSub(pid), selVal)).appendTo($('#categorySelContainer'));
                totalLength += 3;
            }
        }
    },
    getPath: function(id){
    	var selectedCategoryId;
    	if(id!=null&&id!=undefined){
    		selectedCategoryId = id;
    	}
    	else{
    		selectedCategoryId = $('#categoryId').val();
    	}
    	var p = '';
    	var pid;
    	var totalLength = 3;
    	while(totalLength<=selectedCategoryId.length){
    		pid = selectedCategoryId.substring(0, totalLength);
    		p+= this.getItem(pid).name+'/';
    		totalLength += 3;
    	}
        return p;
    }
};
window.doCategorySelChange = CategoryUtil.doCategorySelChange;